class Rudoku::CandidateSelector
  @@candidate_selectors = {}
  def self.[](id)
    @@candidate_selectors[id]
  end
  
  attr_reader :id, :name, :desc, :reason, :logic, :fallback
  
  def initialize(params, &logic)
    # add all rules to the rules hash
    @id, @name, @desc, @reason, @fallback = params[:id], params[:name], params[:desc], params[:reason], params[:fallback]
    @name ||= @id.to_s.gsub("_", " ")
    @logic = logic
    @@candidate_selectors[@id] = self if @id
  end
  
  def select(field)
    selection = run(field)
    selection = @fallback.select(field) if selection.candidate.nil? && @fallback
    selection
  end
  
  def run(field)
    CandidateSelectorLogicDslProxy.new(self, field).run.candidate_selection
  end
end
